#include "TLSContext.hpp"
#include "TLSConnection.hpp"



#include "TCPSocket.hpp"
#include <iostream>

#define PORT 4433
#define SERVER_IP "127.0.0.1"

int main() {
    try {
        // 初始化 TLS 上下文（客户端不需要证书）
        TLSContext ctx(TLSContext::Role::Client, "sm2.cert.pem", "sm2.key.pem");

        // 创建 socket
        TCPSocket tcp_socket;
        tcp_socket.connect(SERVER_IP, PORT);
        TLSConnection conn(ctx.get(), tcp_socket.getFd());

        if (!conn.doHandshake(false)) {
            std::cerr << "Handshake failed\n";
            return 1;
        }

        // 发送数据
        const char* msg = "Hello from client";
        conn.write(msg, strlen(msg));

        // 读取响应
        char buf[1024] = {0};
        int len = conn.read(buf, sizeof(buf));
        std::cout << "Server says: " << std::string(buf, len) << "\n";

    } catch (const std::exception& ex) {
        std::cerr << "Fatal error: " << ex.what() << "\n";
        return 1;
    }

    return 0;
}
